package cmu.mobile.lab;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MyFashionProfile extends Activity{
	ImageView profilePicture;
	Button rightArrow;
	Button leftArrow;
	//String userDisplayName="Set Name";
	//String imagePath;
	TextView nameText;
	SharedPreferences dashboard;
	String picturePath;
	//Intent i;
	Intent result;
	TextView lovePercent;
	TextView ehPercent;
	TextView xPercent;
	LinearLayout responded;
	LinearLayout notResponded;
	MMSContent item;
	String itemStore;
	String itemSize;
	String itemPrice;
	String itemFriendName;
	String itemComment;
	String itemRating;
	int itemID;
	String itemPath;
	String itemPhoneNumber;
	TextView itemInfo;
	TextView advicePerson;
	TextView myComment;
	ImageView friendItem;
	ArrayList<MMSContent> rating;
	ArrayList<MMSContent> all;
	int i;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_myfashionprofile);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		result = new Intent();
		lovePercent = (TextView)findViewById(R.id.lovePercent);
		ehPercent = (TextView)findViewById(R.id.ehPercent);
		xPercent = (TextView)findViewById(R.id.xPercent);
		itemInfo = (TextView)findViewById(R.id.store_info);
		advicePerson = (TextView)findViewById(R.id.name_entry);
		myComment = (TextView)findViewById(R.id.my_message);
		friendItem = (ImageView)findViewById(R.id.item_pic);
		
		/*
		 * if they rated a friend's item, then the rating (response)
		 * should be displayed
		 */
		Database mDbHelper = new Database(MyFashionProfile.this);
		// TODO: add to Database
		mDbHelper.open();
		//friends= mDbHelper.getAllFriendsItems();
		//rating = mDbHelper.getRating(3); 
		all = mDbHelper.getAllMyItems();
		mDbHelper.close();
		
		if( !((all.size()-1) == 0) )
		{
			populatePage(0);
		}
		
		findViewById(R.id.left_button).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(i > 0 )
				{
					i--;
					populatePage(i);
				}
			}
		});
		
		findViewById(R.id.right_button).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(i < (all.size()-1) )
				{
					i++;
					populatePage(i);
				}
			}
		});
		
		
		// back button
		findViewById(R.id.BackButton).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setResult(Activity.RESULT_CANCELED);
				finish();
			}
		});
		
		
		
    }
    public void calculatePercent(int itemID)
	{
    	Database mDbHelper = new Database(MyFashionProfile.this);
    	mDbHelper.open();
		//friends= mDbHelper.getAllFriendsItems();
		rating = mDbHelper.getRating(itemID); 
		//all = mDbHelper.getAllMyItems();
		mDbHelper.close();
		
    	double totalLove = 0;
    	int percentLove = 0;
    	double totalEh = 0;
    	int percentEh = 0;
    	double totalX = 0;
    	int percentX = 0;
    	double totalRatings = 0;
    	MMSContent myItem;
    	
    	//if(rating.size() > 0)
    	//{

			for(int i = 0; i < rating.size(); i++)
    		{
				myItem = rating.get(i);
    			if(myItem.getRating().equals(MMSContent.GREAT))
    			{
    				totalLove++;
    				totalRatings++;
    			}
    			else if(myItem.getRating().equals(MMSContent.SOSO))
    			{
    				totalEh++;
    				totalRatings++;
    			}
    			else if(myItem.getRating().equals(MMSContent.NOWAY))
    			{
    				totalX++;
    				totalRatings++;
    			}			
        		percentLove = ((int) (totalLove / totalRatings)) * 100;
        		percentEh = ((int) (totalEh / totalRatings)) * 100;
        		percentX = ((int) (totalX / totalRatings)) * 100;
        		
        		lovePercent.setText(percentLove + "%");
        		ehPercent.setText(percentEh + "%");
        		xPercent.setText(percentX + "%");
    		}
    //	}
	}
	
	public void populatePage(int i){
	    	item = all.get(i);			
			itemRating = item.getRating();
			itemComment = item.getItemComment();
			itemPath = item.getImagePath();
			itemSize = item.getSize();
			itemPrice = item.getPrice();
			itemStore = item.getStore();
			itemID = item.getItemID();
			calculatePercent(itemID);
				
			
			if( itemSize == null )
				itemSize = "N/A";
			
			if( itemPrice == null )
				itemPrice = "N/A";
			
			if( itemStore == null )
				itemStore = "N/A";
			itemInfo.setText("Store: " + itemStore + " | Price: " + itemPrice + " | Size: " + itemSize);
			
			if (itemComment != null)
				myComment.setText(itemComment);
			else
				myComment.setText("No Comment Available.");
			
			if(itemPath != null)
			{
				Uri u = Uri.fromFile(new File(itemPath));
				friendItem.setImageURI(u);
			}
			else
				friendItem.setImageResource(R.drawable.girl);
			
			
	    }
	
}
